package com.manpower;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.widget.Toast;

public class Receiver extends BroadcastReceiver {

	public static AlertDialog dialog;

	@Override
	public void onReceive(final Context context, Intent intent) {
		String intentAction = intent.getAction();

		if (dialog != null && dialog.isShowing())
			return;
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo net = connectivityManager.getActiveNetworkInfo();

		if (net == null) {
		} else if(State.CONNECTED==net.getState() ){
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle("提示 ");
			alertDialog.setMessage("检测"+net.getTypeName()+"网络可用，是否提交未上传数据");
			alertDialog.setNeutralButton("是",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							Toast.makeText(context, "现在开始上传未上传数据", 1).show();
						}
					});
			alertDialog.setNegativeButton("否",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					});
			dialog = alertDialog.show();
		}

	}

	public static Receiver registerReceiver(Context context) {
		IntentFilter intentfilter = new IntentFilter();
		intentfilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		intentfilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
		intentfilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);

		Receiver receiver = new Receiver();
		context.registerReceiver(receiver, intentfilter);

		return receiver;
	}

}
